package com.etcn.common.mybatis;

import java.util.Properties;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.github.pagehelper.PageHelper;

/**
 * MyBatis基础配置
 */
@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = {"com.etcn.modules.busi.**.dao"}, sqlSessionTemplateRef = "busiSqlSessionTemplate")
public class BusiMyBatisConfig {
	private static final Logger log = LoggerFactory.getLogger(BusiMyBatisConfig.class);

	@Bean
	@Primary
	@ConfigurationProperties(prefix = "busi.datasource")
	public DataSource busiDataSource() {
		return DataSourceBuilder.create().build();
	}

	@Bean
	public SqlSessionFactory busiSqlSessionFactory(@Qualifier("busiDataSource") DataSource dataSource) {
		try {
			SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
			sessionFactoryBean.setDataSource(dataSource);

			// 读取配置
			sessionFactoryBean.setTypeAliasesPackage("com.etcn.modules");
			sessionFactoryBean.setVfs(SpringBootVFS.class);

			// 分页插件
			PageHelper pageHelper = new PageHelper();
			Properties properties = new Properties();
			properties.setProperty("reasonable", "true");
			properties.setProperty("supportMethodsArguments", "true");
			properties.setProperty("returnPageInfo", "check");
			properties.setProperty("params", "count=countSql");
			pageHelper.setProperties(properties);

			Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath:mapper/busi/**/*Mapper.xml");
			sessionFactoryBean.setMapperLocations(resources);
			sessionFactoryBean.setConfigLocation(new DefaultResourceLoader().getResource("classpath:mybatis-config.xml"));

			return sessionFactoryBean.getObject();
		} catch (Exception e) {
			log.error("mybatis sqlSessionFactoryBean create error", e);
			return null;
		}
	}
	
	@Bean
	public DataSourceTransactionManager busiTransactionManager(@Qualifier("busiDataSource") DataSource dataSource) {
		return new DataSourceTransactionManager(dataSource);
	}

	@Bean
	public SqlSessionTemplate busiSqlSessionTemplate(@Qualifier("busiSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
		return new SqlSessionTemplate(sqlSessionFactory);
	}
	
	@Bean(name = "busiJdbcTemplate")
    public JdbcTemplate busiJdbcTemplate(
            @Qualifier("busiDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

}
